package com.example.eurekaconsumerribbonhystrix;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
public class requestController {

    @Resource
    ConsumerService consumerService;

    @GetMapping("doRequest")
    public String doRequest() {
        return consumerService.consumer();
    }
    @Component
    class ConsumerService {
        @Autowired
        RestTemplate restTemplate;

        @HystrixCommand(fallbackMethod = "fallback")
        public String consumer() {
            return restTemplate.getForObject("http://eureka-client/test", String.class);
        }
        //注解中指定的降级逻辑进行执行
        public String fallback() {
            return "fallback";
        }
    }

}
